var util = require('../../../utils/util.js')
var api = require('../../../config/api.js');
const check = require('../../../utils/check.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: "",
    jiaxiao: "请选择驾校",
    jiaxiaoId: 0,
    applyName: '',
    applyPhone: '',
    applyCardNo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  bindSave: function (e) {
    var that = this;
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");
    if (!hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }

    if (that.data.jiaxiaoId == 0) {
      util.showErrorToast('请选择驾校');
      return false;
    }


    if (that.data.applyName == '') {
      util.showErrorToast('请输入姓名');
      return false;
    }

    var applyPhone = that.data.applyPhone;
    if (applyPhone.length == 0) {
      util.showErrorToast('手机号不能为空');
      return false;
    }

    /**
     * 手机号码验证
     */
    if (!check.isValidPhone(applyPhone)) {
      util.showErrorToast('手机号不合法');
      return;
    }


    var cardNo = that.data.applyCardNo;
    if (!cardNo) {
      util.showErrorToast('身份证号不能为空');
      return;
    }
    if (check.isValidIdCard(cardNo) == false) {
      util.showErrorToast('身份证号不合法');
      return;
    }

    wx.showLoading({
      title: '提交中...',
      mask: true,
      success: function () {
      }
    });

    var userid = wx.getStorageSync("userid");
    if(userid == ''){
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return;
    }
    util.request(api.ApplyCoachSave, {
      schoolId: that.data.jiaxiaoId,
      schoolName: that.data.jiaxiao,
      applyName: that.data.applyName,
      applyPhone: that.data.applyPhone,
      applyCardNo: that.data.applyCardNo,
      content: that.data.content,
      uid:userid
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        wx.hideLoading();
        /*wx.showToast({
          icon:'success',
          title: '提交成功',
          success(res) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })*/
        wx.showModal({
          title: '提示',
          content: '提交成功,请等待审核',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          }
        })
        /*wx.showToast({
          title: '提交成功!',
          icon: 'success',
          duration: 3000,
          complete: function () {
            that.setData({
              jiaxiao: "请选择驾校",
              jiaxiaoId: 0,
              applyName:'',
              applyPhone:'',
              applyCardNo:'',
              content: ''
            });
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        });*/
      }else {
        wx.hideLoading();
        util.showErrorToast(res.errmsg);
      }

    });
  },
  contentInput: function (e) {
    this.setData({
      content: e.detail.value,
    });
  },
  selectJX: function () {
    wx.navigateTo({
      url: '/pages/signup/list/list',
    })
  },
  bindNameInput: function (e) {
    this.setData({
      applyName: e.detail.value
    });
  },
  bindPhoneInput: function (e) {
    this.setData({
      applyPhone: e.detail.value
    });
  },
  bindCardNoInput: function (e) {
    this.setData({
      applyCardNo: e.detail.value
    });
  },

})